﻿using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MrytPub
{
    public partial class ShowForm : Form
    {
        string html;
        int fontSize = 30;
        int imgSize = 30;
        public ChromiumWebBrowser browser;

        public ShowForm(string html)
        {
            this.html = html;
            InitializeComponent();
            InitBrowser();
        }

        public void InitBrowser()
        {
            browser = new ChromiumWebBrowser("about：blank");
            this.panel1.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;
            browser.LoadHtml(generateQuestionHtml());
        }

        public String generateQuestionHtml()
        {
            String fontSzie = this.fontSize + "px";
            StringBuilder sb = new StringBuilder();
            sb.Append("<!DOCTYPE html><html><head><meta charset=\"utf-8\" /><style type=\"text/css\"> img{ width:")
                .Append(imgSize)
                .Append("%; height:auto;} .question{ margin:0 auto; ")
                .Append("; font-size:")
                .Append(fontSzie)
                .Append("; text-align:left; } </style> </head> <body> <div class=\"question\">")
                .Append(html)
                .Append("</div></body></html>");
            return sb.ToString();
        }

        private void bt_放大字体_Click(object sender, EventArgs e)
        {
            fontSize += 5;
            browser.LoadHtml(generateQuestionHtml());
        }

        private void bt_缩小字体_Click(object sender, EventArgs e)
        {
            fontSize -= 5;
            browser.LoadHtml(generateQuestionHtml());
        }

        private void bt_放大图片_Click(object sender, EventArgs e)
        {
            imgSize += 5;
            browser.LoadHtml(generateQuestionHtml());
        }

        private void bt_缩小图片_Click(object sender, EventArgs e)
        {
            imgSize -= 5;
            browser.LoadHtml(generateQuestionHtml());
        }

        private void bt_标注_Click(object sender, EventArgs e)
        {
            Image myImage = screenCut(this.PointToScreen(panel1.Location), panel1.Width, panel1.Height);

            PaintPanel pp = new PaintPanel(new Bitmap(myImage));
            pp.Name = "pp";
            this.panel1.Controls.Add(pp);
            pp.BringToFront();
        }

        public Image screenCut(Point p, int width, int height)
        {
            Image img = null;
            try
            {
                img = new Bitmap(width, height);
                using (Graphics gc = Graphics.FromImage(img))
                    gc.CopyFromScreen(p, new Point(0, 0), new Size(width, height));
            }
            catch (Exception) { }

            return img;
        }
    }
}
